#include <iostream>
#include <fstream>
#include <vector>
#include "util/enter_point.hpp"
#include "util/vecs_io.hpp"
#include "util/graph_io.hpp"
#include <random>


std::vector<std::vector<int>> load_ssg(const char *filename) {
    std::ifstream in(filename, std::ios::binary);
    if (!in.is_open()) {
        std::cerr << "Open file error" << std::endl;
        exit(-1);
    }
    int width, n_ep;
    std::vector<int> eps_;
    in.read((char *) &width, sizeof(int));
    in.read((char *) &n_ep, sizeof(int));
    eps_.resize(n_ep);
    in.read((char *) eps_.data(), n_ep * sizeof(int));
    std::vector<std::vector<int>> graph;
    while (!in.eof()) {
        int k;
        in.read((char *) &k, sizeof(int));
        if (in.eof()) {
            break;
        }
        std::vector<int> tmp(k);
        in.read((char *) tmp.data(), k * sizeof(int));
        graph.push_back(tmp);
    }
    return graph;
}

using namespace std;
using namespace GraphGradient;

int main(int argc, char **argv) {
    if (argc != 2) {
        std::cout << "./run dataset-name"
                  << std::endl;
        exit(-1);
    }
    char *dataset_name = argv[1];
    char path[256];
    sprintf(path, "/home/zhengbian/SSG/graph/%s.ssg", dataset_name);

    vector<vector<int>> graph = load_ssg(path);
//    for (int i = 0; i <= 10; i++) {
//        printf("%d\n", (int) graph[i].size());
//    }

    int n_base, vec_dim;
    sprintf(path, "../data/%s/base.fvecs", dataset_name);
    float *base = load_vecs<float>(path, n_base, vec_dim);

//    int enter_point = get_ep_centroid(graph, base, 100, vec_dim);
    int enter_point = rand() / n_base;

    sprintf(path, "../data/%s", dataset_name);
    save_graph(graph, base, vec_dim, "SSG-random-ep", path, enter_point);
    return 0;
}
